<?php
class User_model extends CI_Model
{
    private $table   = 'user';
    private $table_2 = 'user_account';
    private $table_5 = 'macs';
    
    public function findById($uid)
    {
        $this->db->where('uid', $uid);
        return $this->db->get($this->table);
    }
    
    public function total($params=array())
    {
        $this->db->from($this->table.' as user');
        $this->db->join($this->table_2.' as user_account', 'user_account.uid = user.uid');
        if (!empty($params['username'])) {
            $this->db->where("((`user`.`user_name` LIKE '%{$params['username']}%') OR (`user`.`alias_name` LIKE '%{$params['username']}%') OR (`user`.`uid`='{$params['username']}'))");
        }
        if (!empty($params['uidid'])) {
            $this->db->where('user.uid', $params['uidid']);
        }
        if (!empty($params['user_type'])) {
            $this->db->where('user.user_type & '.$params['user_type'].'=', $params['user_type']);
        }
        if (!empty($params['parent_id'])) {
            $this->db->where('user.parent_id', $params['parent_id']);
        }
        if (!empty($params['mobile_phone'])) {
            $this->db->where('user.mobile_phone', $params['mobile_phone']);
        }
        if (!empty($params['flag'])) {
            $this->db->where('user.flag', $params['flag']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where('user.created_at >= ', $params['start_date'].' 00:00:00');
        }
        if (!empty($params['end_date'])) {
            $this->db->where('user.created_at <= ', $params['end_date'].' 23:59:59');
        }
        return $this->db->count_all_results();
    }
    
    public function page_list($page_num, $num, $params=array())
    {
        $this->db->select('user.*, user_account.over_beizhu, user_account.amount_carry, user_account.amount_bonus, user_account.amount_month, user_account.outlay_total, user_account.income_total');
        $this->db->from($this->table.' as user');
        $this->db->join($this->table_2.' as user_account', 'user_account.uid = user.uid');
        if (!empty($params['username'])) {
            $this->db->where("((`user`.`user_name` LIKE '%{$params['username']}%') OR (`user`.`alias_name` LIKE '%{$params['username']}%') OR (`user`.`uid`='{$params['username']}'))");
        }
        if (!empty($params['uidid'])) {
            $this->db->where('user.uid', $params['uidid']);
        }
        if (!empty($params['user_type'])) {
            $this->db->where('user.user_type & '.$params['user_type'].'=', $params['user_type']);
        }
        if (!empty($params['parent_id'])) {
            $this->db->where('user.parent_id', $params['parent_id']);
        }
        if (!empty($params['mobile_phone'])) {
            $this->db->where('user.mobile_phone', $params['mobile_phone']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where('user.created_at >= ', $params['start_date'].' 00:00:00');
        }
        if (!empty($params['end_date'])) {
            $this->db->where('user.created_at <= ', $params['end_date'].' 23:59:59');
        }
        $this->db->order_by('user.uid', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    public function insert($postData)
    {
        $data = array(
            'user_name'    => $postData['user_name'],
            'alias_name'   => $postData['alias_name'],
            'mobile_phone' => !empty($postData['mobile_phone']) ? $postData['mobile_phone'] : '',
            'user_type'    => $postData['user_type'],
            'parent_id'    => !empty($postData['parent_id']) ? $postData['parent_id'] : 16,
            'owner_id'     => !empty($postData['parent_id']) ? $postData['parent_id'] : 16,
            'pw'           => md5($postData['pw']),
            'flag'         => 1,
            'sms'          => !empty($postData['sms']) ? $postData['sms'] : 0,
            'key'          => '',
            'login_count'  => 0,
            'contacter'   => $postData['contacter'],
        );
        if (!empty($postData['user_email'])) {
            $data['user_email'] = $postData['user_email'];
        }
        if (!empty($postData['cellphone'])) {
            $data['cellphone'] = $postData['cellphone'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function update($postData)
    {
        $data = array(
            'user_name'    => $postData['user_name'],
            'alias_name'   => $postData['alias_name'],
            'mobile_phone' => !empty($postData['mobile_phone']) ? $postData['mobile_phone'] : '',
            'user_type'    => $postData['user_type'],
            'parent_id'    => !empty($postData['parent_id']) ? $postData['parent_id'] : 16,
            'flag'         => $postData['flag'],
            'sms'          => !empty($postData['sms']) ? $postData['sms'] : 0,
            'contacter'   => $postData['contacter'],
            'user_email' => !empty($postData['user_email']) ? $postData['user_email'] : '',
        );
        if (!empty($postData['cellphone'])) {
            $data['cellphone'] = $postData['cellphone'];
        }
        return $this->db->update($this->table, $data, array('uid'=>$postData['uid']));
    }
    
    public function findByParentIds($parentIds = array())
    {
        $this->db->where_in('parent_id', $parentIds);
        return $this->db->get($this->table);
    }
    
    /**
     * 更新用户表信息
     * @param unknown $postData
     */
    public function updateUserInfo($postData = array())
    {
        if (!empty($postData['flag'])) {
            $data['flag'] = $postData['flag'];
        }
        return $this->db->update($this->table, $data, array('uid'=>$postData['uid']));
    }
    
    function resetpwd($uid)
    {
        $data = array(
            'pw' => md5('123456'),
        );
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }
    
    public function findByUserName($postData)
    {
        if (!empty($postData['user_name'])) {
            $this->db->where('user_name', $postData['user_name']);
            if (!empty($postData['alias_name'])) {
                $this->db->or_where('alias_name', $postData['alias_name']);
            }
        }
        return $this->db->get($this->table);
    }
    
    /**
     * 验证用户名
     * @param unknown $userName
     */
    public function validateName($userName)
    {
        $this->db->where('user_name', $userName);
        return $this->db->get($this->table);
    }
    
    /**
     * 验证手机号码
     * @param unknown $userName
     */
    public function validateMobilePhone($mobilePhone)
    {
        $this->db->where('mobile_phone', $mobilePhone);
        return $this->db->get($this->table);
    }
    
    public function findByAutocomplete()
    {
        $this->db->select('uid, user_name, alias_name');
        $this->db->where('user.user_type & '.UTID_CUSTOMER.'!=', UTID_CUSTOMER);
        return $this->db->get($this->table);
    }
    
    public function findByIds($data)
    {
        if (!empty($data['uid'])) {
            $this->db->where_in('uid', $data['uid']);
        }
        if (!empty($data['user_name'])) {
            $this->db->where_in('user_name', $data['user_name']);
        }
        if (!empty($data['user_type'])) {
            $this->db->where('user_type & '.$data['user_type'].'=', $data['user_type']);
        }
        if (!empty($data['parent_id'])) {
            if (is_array($data['parent_id'])) {
                foreach ($data['parent_id'] as $key=>$value) {
                    if ($key == 0) {
                        $this->db->where('parent_id', $value);
                    } else {
                        $this->db->or_where('parent_id', $value);
                    }
                }
            } else {
                $this->db->where('parent_id', $data['parent_id']);
            }
        }
        return $this->db->get($this->table);
    }
    
    public function findByMacs($uid)
    {
        $this->db->select('macs.mark, mac_id');
        $this->db->from($this->table.' AS user');
        $this->db->join($this->table_5.' AS macs','macs.teller_uid=user.uid','inner');
        foreach ($uid as $key=>$value) {
            if ($key == 0) {
                $this->db->where('parent_id', $value);
            } else {
                $this->db->or_where('parent_id', $value);
            }
        }
        $this->db->where('user_type', '65536');
        
        return $this->db->get();
    }
    
    public function findByUid($uid)
    {
        $this->db->select('*');
        $this->db->from($this->table.' as user');
        $this->db->join($this->table_2.' as user_account', 'user_account.uid = user.uid');
        $this->db->where('user.uid', $uid);
        $result = $this->db->get();
        if ($result->num_rows() > 0) {
            return $result->row();
        }
        return false;
    }
    
    public function validateAccountPassword($uid, $password)
    {
        $this->db->select('*');
        $this->db->from($this->table.' as user');
        $this->db->join($this->table_2.' as user_account', 'user_account.uid = user.uid');
        $this->db->where('user.uid', $uid);
        $this->db->where('user.pw', md5($password));
        $result = $this->db->get();
        if ($result->num_rows() > 0) {
            return $result->row();
        }
        return false;
    }
    
    /**
     * 用户注册的总人数
     * @param array $data
     * @return int 
     */
    public function userRegParentTotal($data = array())
    {
        $this->db->select('COUNT(DISTINCT parent_id) as num');
        $this->db->from($this->table);
        $this->db->where_not_in('uid', array(1,4,16,64,256,1024,4096));
        if (!empty($data['parent_id'])) {
            $this->db->where('uid',$data['parent_id']);
        }
        $result = $this->db->get();
        return $result->row()->num;
    }
    
    public function userRegTotal($data = array())
    {
        $this->db->select('uid');
        $this->db->from($this->table);
        $this->db->where_not_in('uid', array(1,4,16,64,256,1024,4096));
        if (!empty($data['parent_id'])) {
            $this->db->where('parent_id', $data['parent_id']);
        }
        return $this->db->count_all_results();
    }
    
    public function findByRegs($num, $data = array())
    {
        $this->db->select('COUNT(uid) AS num, parent_id,(SELECT u.user_name FROM `user` AS u WHERE `u`.uid=`user`.`parent_id`) AS user_name,(SELECT u.alias_name  FROM `user` AS u WHERE `u`.uid=`user`.`parent_id`) AS alias_name');
        $this->db->from($this->table.' as user');
        $this->db->where_not_in('uid', array(1,4,16,64,256,1024,4096));
        if (!empty($data['parent_id'])) {
            $this->db->where('parent_id', $data['parent_id']);
        }
        $this->db->group_by('parent_id');
        $this->db->order_by('num DESC');
        $this->db->limit(20,$num);
        return $this->db->get();
    }
    
    public function findBydetails($num, $data)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        if (!empty($data['user_name'])) {
            $this->db->where_in('user_name', $data['user_name']);
        }
        if (!empty($data['parent_id'])) {
            $this->db->where('parent_id', $data['parent_id']);
        }
        $this->db->limit(20, $num);
        return $this->db->get();
    }
    
    /**
     * 联合查询账户信息(user表和user_detail表)
     */
    public function selectUserDetailInfo($where,$select = '*')
    {
        $this->db->select($select);
        $this->db->from($this->table);
        $this->db->where($where);
        
        return $this->db->get();
    }
    
    /**
     * 修改用户表
     * @param unknown $where
     * @param unknown $postData
     */
    public function updateUser($where, $postData)
    {
        $this->db->where($where);
        return $this->db->update($this->table, $postData);
    }
}